package com.example.demo.utils;

import com.example.demo.annotation.CheckField;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Author: LJP
 * @Description:        字段校验类
 * @Date: Created in 17:05 2021/11/30
 */
public class CheckObjectUtil {

    public static <T> Map<Boolean,String> checkFiledIsNull(T t) throws IllegalAccessException {
        Map<Boolean,String> map = new HashMap<>();
        //通过反射获取所有的字段
        for (Field field : t.getClass().getDeclaredFields()) {
            //从字段上获得注解
            CheckField checkField = field.getAnnotation(CheckField.class);
            //判断注解是否存在
            if (checkField!=null){
                field.setAccessible(true);
                if (field.get(t) == null || (field.get(t).toString() == null || (field.get(t).toString().trim().length() == 0))) {
                    map.put(false,checkField.msg());
                    return map;
                }
            }
        }
        map.put(true,"字段都不为空");
        return map;
    }
}
